package mytest;

public class Test1 {

	static class Node{
		int left,right;
		int num;
	}
	
	static int max=10000;
	static Node[] arr=new Node[4*max];
	
	static {
		for(int i=0;i<arr.length;i++)
			arr[i]=new Node();
	}
	
	static void build(int index,int left,int right)
	{
		arr[index].left=left;
		arr[index].right=right;
		arr[index].num=right-left+1;
		if(left==right)return;
		int mid=(left+right)>>1;
		build(index*2,left,mid);
		build(index*2+1,mid+1,right);
	}
	
	static int update(int index,int k)
	{
		arr[index].num--;
		if(arr[index].left==arr[index].right)
		{
			return arr[index].left;
		}
		
		if(arr[index*2].num>=k)
		{
			return update(index*2,k);
		}else {
			k-=arr[index*2].num;
			return update(index*2+1,k);
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		//int a[]=new int[] {1,2,3,4,5,6,7};
		int b[]=new int[] {4,-4,4,-3,4,4,4};
		
		int start= 2;
		
		build(1, 1, b.length);
		
		for(int i=1;i<=b.length;i++)
		{
			int k=update(1,start);
			System.out.println(k);
			if(i==b.length)break;
			if(b[k-1]>0)start=(start+b[k-1]-1)%(b.length-i);
			else start=((start+b[k-1])%(b.length-i)+b.length-i)%(b.length-i);
			if(start==0)start=b.length-i;
		}

	}

}
